<template>
  <view v-if="goodsDetail.goods_name">
    <!-- 图片 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item,index) in goodsDetail.pics" :key="index">
        <image :src="item.pics_mid_url"  @click="clickSwiper(index)"></image>
      </swiper-item>
    </swiper>
    <!-- 价格 -->
    <view class="priceInfo">
      <view class="price">¥{{goodsDetail.goods_price}}</view>
      <!-- 图标 -->
      <view class="icons">
        <u-icon class="item" type="tune"></u-icon>
        <u-icon class="item" type="star"></u-icon>
      </view>
    </view>
    <view class="detailInfo u-line-2">
      {{goodsDetail.goods_name}}
    </view>
    <!-- 图文详情 -->
    <view class="picDetail">
      图文详情
    </view>
    <!-- 富文本 -->
    <view class="u-content">
      <rich-text :nodes="goodsDetail.goods_introduce"></rich-text>
    </view>
    <!-- 底部订单提交栏 -->
    <view class="goods_nav">
      <uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" 
      @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {mapState,mapMutations,mapGetters} from 'vuex'
  export default {
    computed:{
      ...mapState('cart',['cart']),
      ...mapGetters('cart',['total'])
    },
    data() {
      return {
        goodsDetail: {}, // 商品信息
        options: [{
        			icon: 'headphones',
        			text: '客服'
        		},{
        			icon: 'cart',
        			text: '购物车',
        			info: 0
        		}],
        	    buttonGroup: [{
        	      text: '加入购物车',
        	      backgroundColor: '#ff0000',
        	      color: '#fff'
        	    },
        	    {
        	      text: '立即购买',
        	      backgroundColor: '#ffa200',
        	      color: '#fff'
        	    }
        	    ]

      }
    },
    methods: {
      ...mapMutations('cart',['addToCart']),
      async getGoodsDetail(goods_id) {
        const {
          message
        } = await this.$u.get("/goods/detail", {
          goods_id
        })
        console.log(message)
        message.goods_introduce=message.goods_introduce.replace(/<img /g,'<img style="display:block;"').
        replace(/webp/g, 'jpg')
        this.goodsDetail = message
        
      },
      //点击切换大图
      clickSwiper(index) {
        console.log(index);
        uni.previewImage({
          urls: this.goodsDetail.pics.map(item => item.pics_big_url),
          current: index
        })
      },
      // 左侧按钮的点击事件处理函数
      onClick(e) {
        if (e.content.text === '购物车') {
          // 切换到购物车页面
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      //点击加入购物方法
      buttonClick(e){
        if(e.content.text==='加入购物车'){
            // 2. 组织一个商品的信息对象
                const goods = {
                   goods_id: this.goodsDetail.goods_id,       // 商品的Id
                   goods_name: this.goodsDetail.goods_name,   // 商品的名称
                   goods_price: this.goodsDetail.goods_price, // 商品的价格
                   goods_count: 1,                           // 商品的数量
                   goods_small_logo: this.goodsDetail.goods_small_logo, // 商品的图片
                   goods_state: true                         // 商品的勾选状态
                }
                this.addToCart(goods)
        }
      }
    },
    watch:{
      total:{
        handler(newVal){
          const findResult=this.options.find(item=>item.text==='购物车')
           if(findResult){
             findResult.info=newVal
           }
        },
        immediate:true
      }
    },
    onLoad(options) {
      const goods_id = options.goods_id
      this.getGoodsDetail(goods_id)
    }
  }
</script>

<style lang="scss" scoped>
  swiper {
    height: 750rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .priceInfo {
    display: flex;
    justify-content: space-between;
    margin: 19rpx 38rpx;
    font-size: 36rpx;

    .price {
      color: #EA4350;
    }

    .icons {
      color: #707070;
    }

    .item {
      margin: 0 23rpx;
    }
  }

  .detailInfo {
    color: #434343;
    font-size: 30rpx;
    margin: 0 33rpx 48rpx 33rpx;
  }

  .picDetail {
    font-weight: bold;
    font-size: 28rpx;
    color: #434343;
    margin-top: 38rpx;
    text-align: center;
    border-top: 20rpx solid #f9f9f9;
  }

  .u-content {
    margin-bottom: 130rpx;
  }
  .goods_nav{
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }
</style>
